const axios = require('axios');
const { Activity } = require('./models');

async function createLiveActivityAndTest() {
  try {
    console.log('=== 创建实时活动并测试签到流程 ===');
    
    // 1. 登录Alice
    const loginResponse = await axios.post('http://localhost:3000/api/auth/login', {
      email: 'alice@student.com',
      password: 'password123'
    });
    
    const token = loginResponse.data.token;
    console.log('✅ Alice登录成功');
    
    // 2. 创建一个未来的活动
    const now = new Date();
    const startTime = new Date(now.getTime() + 1000 * 60 * 5); // 5分钟后开始
    const endTime = new Date(now.getTime() + 1000 * 60 * 60 * 2); // 2小时后结束
    
    const activityData = {
      title: '实时签到测试活动',
      description: '用于测试签到功能的实时活动',
      location: '测试地点',
      start_time: startTime.toISOString(),
      end_time: endTime.toISOString(),
      max_participants: 50,
      category_id: 1
    };
    
    const createResponse = await axios.post('http://localhost:3000/api/activities', activityData, {
      headers: { 'Authorization': `Bearer ${token}` }
    });
    
    const activityId = createResponse.data.activity.id;
    console.log('✅ 活动创建成功, ID:', activityId);
    
    // 3. Alice参与活动
    await axios.post(`http://localhost:3000/api/activities/${activityId}/join`, {}, {
      headers: { 'Authorization': `Bearer ${token}` }
    });
    console.log('✅ Alice参与活动成功');
    
    // 4. 将活动更新为正在进行状态
    await Activity.update({
      start_time: new Date(now.getTime() - 1000 * 60 * 10), // 10分钟前开始
      end_time: new Date(now.getTime() + 1000 * 60 * 60 * 2) // 2小时后结束
    }, {
      where: { id: activityId }
    });
    console.log('✅ 活动已更新为进行中状态');
    
    // 5. Alice签到
    console.log('\n开始签到...');
    const checkinResponse = await axios.post(`http://localhost:3000/api/activities/${activityId}/checkin`, {
      latitude: 39.9042,
      longitude: 116.4074,
      verification_code: 'TEST123'
    }, {
      headers: { 'Authorization': `Bearer ${token}` }
    });
    
    console.log('✅ 签到成功!');
    console.log('响应:', checkinResponse.data);
    
    // 6. 等待徽章生成
    console.log('\n等待NFT徽章生成...');
    await new Promise(resolve => setTimeout(resolve, 5000));
    
    // 7. 检查徽章
    const badgesResponse = await axios.get(`http://localhost:3000/api/badges?user_id=${loginResponse.data.user.id}`, {
      headers: { 'Authorization': `Bearer ${token}` }
    });
    
    console.log('✅ 用户徽章数量:', badgesResponse.data.badges.length);
    if (badgesResponse.data.badges.length > 0) {
      const latestBadge = badgesResponse.data.badges[0];
      console.log('最新徽章:', {
        name: latestBadge.badge_name,
        description: latestBadge.badge_description,
        status: latestBadge.status,
        activity: latestBadge.activity?.title
      });
    }
    
    console.log('\n🎉 完整的签到->NFT徽章流程测试成功！');
    
  } catch (error) {
    console.error('❌ 测试失败:', error.message);
    if (error.response) {
      console.error('状态码:', error.response.status);
      console.error('错误详情:', error.response.data);
    }
  }
}

createLiveActivityAndTest(); 